/**
 * +======================================================================================================================+
 * @Author: CimZzz
 * @Date: 2023-03-07 14:56:14
 * 
 * 代码分割块，以下内容涉及: 
 * 
 * C++ 智能指针测试
 * 
 * 1. 嵌套智能指针释放
 */

#include <iostream>
#include <memory>

using namespace std;

class A {
public:
	A(const std::string& name): name(name), parent(nullptr), child(nullptr) {}
	virtual ~A() { printf("%s release\n", name.c_str()); }
	void foo() {
		printf("%s action\n", this->name.c_str());
		if(child.get() != nullptr) {
			child->foo();
			child->parent.reset();
			child.reset();
		}
	}
	std::string name;
	shared_ptr<A> parent;
	shared_ptr<A> child;
};

int main(int argc, char ** argv) {
	{
		auto parent = std::make_shared<A>("parent");
		auto child = std::make_shared<A>("child");
		parent->child = child;
		child->parent = parent;
		printf("child reset\n");
		child.reset();
		parent->foo();
	}
	printf("===============\n");


	return 0;
}